/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Lucas
 */

import com.mysql.jdbc.Driver;
import java.sql.*;


public class BD_Connector {
    
    private     Connection  connection;
    private     ResultSet           resultSet;
            
            
    public BD_Connector () throws SQLException{        
        makeConnection();
    }
        
    public  Connection makeConnection() throws SQLException {
        if (connection == null) {
             new Driver();
            // buat koneksi
             connection = DriverManager.getConnection(
                       "jdbc:mysql://localhost/tcc",
                       "root",
                       "gaita1221");
         }
        
         return connection;
     } 
    
    public ResultSet consultaBase(String SQL) throws SQLException{
        Statement statement = connection.createStatement();
        return statement.executeQuery(SQL);
        
        //writeResultSet(resultSet);       
    }
    
    private void writeResultSet(ResultSet resultSet) throws SQLException {
        String  diaLeitura;
        String  horaLeitura;
        String  idSensor;
        String  situac;
        String  sms;
        int     valor;

        while (resultSet.next()) {

        diaLeitura      =   resultSet.getString("DIA_LEITURA");
        horaLeitura     =   resultSet.getString("HORA_LEITURA");
        idSensor        =   resultSet.getString("IDSENSOR");
        valor           =   resultSet.getInt("VALOR");
        situac          =   resultSet.getString("SITUAC");
        sms             =   resultSet.getString("SMS");

        System.out.println( diaLeitura  + ":"       + 
                            horaLeitura + " - "     + 
                            situac      + sms       +   " - " +
                            "Sensor "   + idSensor  +": "  +   valor
                          );
        }
  }
    
    
}
